﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IloveRSS.Models.DataMap
{
    public class TagsMapRepository
    {
        private ILoveRSSDataContext dataContext;

        public TagsMapRepository(ILoveRSSDataContext dt)
        {
            this.dataContext = dt;
        }

        /*
        *  if exist return tag
        *  else return null 
        */
        public Tags ExistByText(string text)
        {
            return dataContext.Tags.FirstOrDefault(tag => tag.Text.Equals(text));
        }

        /*
         * always return tag with text
         */ 
        public Tags Add(String text)
        {
            Tags tag =  ExistByText(text);
            if (null == tag)
            {
                tag = new Tags{ Text = text };
                dataContext.Tags.InsertOnSubmit(tag);
                dataContext.SubmitChanges();
            }
            return tag;
        }


        public IEnumerable<Tags> GetAllTags()
        {
            return dataContext.Tags;
        }
    }
}